<?php
/** * Analytics Class * 
 * @package TIAP 
 * @subpackage Controller 
 * @category Crime Report 
 * @author Harits H Balfas 
 * @description Handling analytics function for crime report
 * @link http://twitter.com/jeblagsancang 
 */
class Analytics extends Controller {
    function Analytics(){
        parent::Controller();        
        $this->load->plugin('graph');
        $this->load->library('opgraph');
        $this->load->helper(array('form','tiap_helper','html'));        
        $this->load->model(array('Crime_report_model'));
    }
    
    function index(){
        //$data['crime_data'] = $this->Crime_report_model->graph();
        $submit_button = $this->input->post('submit'); 
        if($submit_button != ''){
            $startMonth = $this->input->post('start_month');
            $endMonth = $this->input->post('end_month');
            //$months = $this->input->post('months');
            $graph = open_flash_chart_object_str( '600', '300', site_url('analytics/graph_crime/'.$startMonth.'/'.$endMonth), false, base_url() );    
            
        } else {
            $graph = open_flash_chart_object_str( '600', '300', site_url('analytics/graph_crime'), false, base_url() );    
        }
        
        if ($startMonth == ''){
            $endMonth= date('mY');
            $startMonth = date("mY", mktime(0, 0, 0, (date(m)-1), 30, date("Y"))); 
        }
        
        
        $data['graph'] = $graph;
        $breadcrumb_list = array(
            array('name'=>'Analytics', 'href'=>site_url('analytics')),
        );
        $data['startMonth'] = $startMonth;
        $data['endMonth'] = $endMonth;
        $data['rangeData'] = $rangeData;
        $dateSearch = date("Y-m", mktime(0, 0, 0, (date(m)-$i), 30, date("Y")));
        $data['dateSearch'] = $dateSearch;
        $data['monthsList'] = array(null, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
        $data['breadcrumb_list'] = $breadcrumb_list;
        //echo "<pre><font color='#999999'>";print_r($data); echo "</font></pre>";
        $this->load->view(TEMPLATE_NAME.'/front/analytics',$data);
    }
    
    function graph_crime($startMonth = '', $endMonth = ''){   
        
        /*if(empty($months)){
            $months = date('n');    
        }*/
        
        if ($startMonth == ''){
            $endMonth= date('mY');
            $startMonth = date("mY", mktime(0, 0, 0, (date(m)-1), 30, date("Y"))); 
        } 
        if ($startMonth < $endMonth){
            $executeLimit = true;
            if (substr($startMonth, -4) < substr($endMonth, -4)){
                $rangeData = (12 - substr($startMonth, 0,2))+substr($endMonth, 0,2);
            }else{
                $rangeData = substr($endMonth, 0,2) - substr($startMonth, 0,2);
            }
            
        }
        
        $this->opgraph->opgraph();        
        $this->opgraph->title( 'Crime Analytics ', '{font-size: 26px;}' );
        $colorList = array('FF0000','00FF00','0000FF','000000','FFFFFF','BBCCFF','1122CC','33F','F60','969','F6C','CFC');
        for ($i = 0; $i <= $rangeData; $i++){           
            if (!$executeLimit){
                $dateSearch = date("Y-m", mktime(0, 0, 0, (date(m)-$i), 30, date("Y")));
            }else{
                $dateSearch = date("Y-m", mktime(0, 0, 0, (substr($endMonth, 0,2)-$i), 30, substr($endMonth, -4)));
                 
            }            
            $data = $this->Crime_report_model->graph($dateSearch);
            
            $this->opgraph->set_data($data); 
            $this->opgraph->line_hollow( 2, 4, $colorList[$i], $dateSearch, 9 );              
        }       

        
        //$this->opgraph->set_data( $data2 ); 
        //$this->opgraph->line_hollow( 2, 4, '0xCC9933', 'dua', 9 );
        
        // label each point with its value         
        $numberofdays = intval(date("t"));
        $xlabel = array();
        for( $i=1; $i<=$numberofdays; $i++ ){
          $xlabel[] = $i;
        }
        //$this->opgraph->set_x_labels( array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec' ) );
        $this->opgraph->set_x_labels( $xlabel );
        
        // set the Y max
        $this->opgraph->set_y_max( 60 );
        // label every 20 (0,20,40,60)
        $this->opgraph->y_label_steps( 6 );

        // display the data        
        echo $this->opgraph->render();            
    }
    
}
?>
